﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Info : System.Web.UI.Page
{
    //DropDownList数据源集合
    private List<MyItem> myItems = new List<MyItem>();

    protected void Page_Init(object sender, EventArgs e)
    {
        CreateData();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //定义下拉列表显示项和相关属性
        DdlProvnice.DataTextField = "ItemText";
        DdlProvnice.DataValueField = "ItemValue";
        DdlCity.DataTextField = "ItemText";
        DdlCity.DataValueField = "ItemValue";

        if (!IsPostBack)
        {
            //lblTrueName.CssClass = "red";
            //ddlJiGuan.Items.Add("天津市");
            //ddlProvnice.Items.Add(new ListItem("天津市","tj"));

            Predicate<MyItem> _p = _f;
            DdlProvnice.DataSource = myItems.FindAll(_p);
            DdlProvnice.DataBind();
            DdlProvnice.SelectedIndex = 0;

            DdlCity.DataSource = myItems.FindAll(delegate (MyItem myItem) {
                return myItem.ItemParent == DdlProvnice.SelectedValue;
            });
            DdlCity.DataBind();

            LbSkill.Items.Add(new ListItem("C语言", "c"));
            LbSkill.Items.Add(new ListItem("Java语言", "java"));
            LbSkill.Items.Add(new ListItem("C#语言", "c#"));

            BtnSubmit.Enabled = false;

            ImgAvtar.ImageUrl = "http://pic2.zhimg.com/50/v2-fb824dbb6578831f7b5d92accdae753a_hd.jpg";

            Literal2.Mode = LiteralMode.Encode;
            Literal2.Text = "<b>Literal可以拼接html字符串";

            //BulletedList1.Items.Add("列表项一");
            //BulletedList1.Items.Add("列表项二");
            //BulletedList1.Items.Add("列表项三");
            List<string> _data = new List<string>() { "列表项一", "列表项二", "列表项三" };
            BulletedList1.DataSource = _data;
            BulletedList1.DataBind();
        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx");
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Write("图片按钮提交成功");
    }

    protected void BtnSubmit_Click(object sender, EventArgs e)
    {
        Response.Write("提交成功<br>");
        LblFav.Text = "选择为：";
        foreach (ListItem item in ChklFav.Items)
        {
            if (item.Selected)
            {
                LblFav.Text += item.Text + "&nbsp";
            }
        }
        if (RbMale.Checked)
        {
            LblGender.Text = RbMale.Text;
        }
        if (RbFemale.Checked)
        {
            LblGender.Text = RbFemale.Text;
        }
        if (RblDegree.SelectedItem != null)
        {
            LblDegree.Text = RblDegree.SelectedItem.Text;
        }
    }

    protected void DdlProvnice_SelectedIndexChanged(object sender, EventArgs e)
    {
        DdlCity.Items.Clear();
        DdlCity.DataSource = myItems.FindAll(myItem => myItem.ItemParent == DdlProvnice.SelectedValue);
        DdlCity.DataBind();
    }

    private void CreateData()
    {
        myItems.Add(new MyItem("bj", "北京市", "0"));
        myItems.Add(new MyItem("sh", "上海市", "0"));
        myItems.Add(new MyItem("jx", "江西省", "0"));
        myItems.Add(new MyItem("gd", "广东省", "0"));
        myItems.Add(new MyItem("hb", "湖北省", "0"));

        myItems.Add(new MyItem("haidian", "海淀区", "bj"));
        myItems.Add(new MyItem("xujiahui", "徐家汇", "sh"));
        myItems.Add(new MyItem("nanchang", "南昌市", "jx"));
        myItems.Add(new MyItem("jiujiang", "九江市", "jx"));
        myItems.Add(new MyItem("guangzhou", "广州市", "gd"));
        myItems.Add(new MyItem("wuhan", "武汉市", "hb"));

    }

    private bool _f(MyItem item)
    {
        return item.ItemParent == "0";
    }

    protected void LbSkill_SelectedIndexChanged(object sender, EventArgs e)
    {
        LblSkill.Text = "";
        for (int i = 0; i < LbSkill.Items.Count; i++)
        {
            if (LbSkill.Items[i].Selected)
            {
                LblSkill.Text += LbSkill.Items[i].Text;
            }
        }
    }

    protected void ChkbAgree_CheckedChanged(object sender, EventArgs e)
    {
        if (ChkbAgree.Checked)
        {
            BtnSubmit.Enabled = true;
        }
        else
        {
            BtnSubmit.Enabled = false;
        }
    }
}

class MyItem
{
    private string itemValue;
    private string itemText;
    private string itemParent;

    public MyItem(string itemValue, string itemText, string itemParent)
    {
        this.itemValue = itemValue;
        this.itemText = itemText;
        this.itemParent = itemParent;
    }

    public string ItemValue { get => itemValue; set => itemValue = value; }
    public string ItemText { get => itemText; set => itemText = value; }
    public string ItemParent { get => itemParent; set => itemParent = value; }
}